/*
 * AccordionItem.fx
 *
 * Created on Feb 28, 2010, 8:58:39 PM
 */
package com.gpsbuddy.components;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.geometry.VPos;
import javafx.geometry.HPos;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import com.gpsbuddy.main.Enviroment;

/**
 * @author jonycus
 */
public class AccordionItem extends CustomNode {

    public var title: String;
    public var opened: Boolean = false;
    public var width: Number;
    public var height: Number;
    public var content: Node[];
    public var owner: Accordion;
    public var index: Integer;
    public var spacing: Number;
    var titleText = Text {
                content: title;
                fill: Color.WHITE;
                font: Enviroment.font12;
            }
    var hbox = HBox {
                nodeVPos: VPos.CENTER;
                hpos: HPos.CENTER;
                vpos: VPos.CENTER;
                content: [titleText]
                layoutX: 20
                layoutY: 5
            }
    var titleBackground = Rectangle {
                width: bind width;
                height: bind height;
                fill: Color.LIGHTBLUE;
            }
    public var contentGroup = Group {
                translateX: 20
                translateY: 10
                content: [content]
            }
    var group = Group {
                content: [titleBackground, hbox, contentGroup]
            }

    override protected function create(): Node {
        return group;
    }

    override def onMouseClicked = function (event) {
                var openedFound = false;
                var openedItem: AccordionItem;
                for (item: AccordionItem in owner.items) {
                    if (item.opened) {
                        item.opened = false;
                        openedFound = true;
                        openedItem = item;
                        item.contentGroup.visible = false;
                    }
                    if (openedFound) {
                        for (i: AccordionItem in owner.items) {
                            if (i.index > openedItem.index and i.index <= this.index) {
                                i.animate(i.layoutY, i.index * (i.height + i.spacing));
                            }
                        }
                    } else {
                        for (i: AccordionItem in owner.items) {
                            if (i.index > this.index) {
                                i.animate(i.layoutY, owner.height - ((sizeof owner.items - indexof i) * (i.height + spacing)));
                            }
                        }
                    }
                }
                this.contentGroup.visible = true;
                this.opened = true;
            }

    function animate(start: Number, end: Number) {
        Timeline {
            keyFrames: [
                KeyFrame {
                    time: 0s
                    values: [this.layoutY => start tween Interpolator.LINEAR];
                }
                KeyFrame {
                    time: 0.5s
                    values: [this.layoutY => end tween Interpolator.LINEAR];
                }
            ]
        }.play();
    }

}
